<?php

/**
 * 资讯管理
 * @author renpper
 * @date 2014-12-25
 */

namespace Admin\Controller;
class NewsController extends BaseController {
	
	private $news_model, $types, $uid;
	
	public function _initialize(){
		$this->news_model = getInstance('Admin\Model\NewsModel');
		$this->types = $this->getNewsType();
	}
	
	/**
	 * 获取资讯类型
	 */
	protected function getNewsType(){
		if($news_types = getInstance('Admin\Model\NewsTypeModel')->field('id,title')->select()){
			foreach($news_types as $v){
				$data[$v['id']] = $v['title'];
			}
		}
		return $data;
	}
	
	
	/**
	 * 列表页
	 */
	public function index(){
		$page = $this->view->page(count($this->news_model->where('form')->select()));
		$news = $this->news_model->where('form')->order($page['orderFieldStr'])->limit($page['limit'].','.$page['numPerPage'])->select();
		if(is_array($news)){
			$manager_model = getInstance('Admin\Model\ManagerModel');
			foreach($news as &$n){
				$n['type'] = isset($this->types[$n['type']])?$this->types[$n['type']]:'';
				$manager_info = $manager_model->field('username')->where("uid={$n['create_uid']}")->find();
				$n['create_username'] = $manager_info['username'];
			}
		}
		$this->assign('types', $this->types);	
		$this->assign('news', $news);
		$this->assign('page', $page);
		$this->display('Admin/View/News/index.php');
	}
	
	
	/**
	 * 获取条件SQL
	 */
	private function getWhereSql(){
		$create_uid = isset($_REQUEST['create_uid'])?(int)$_REQUEST['create_uid']:'';
		$title = isset($_REQUEST['title'])?$_REQUEST['title']:'';
		$type = isset($_REQUEST['type'])?(int)$_REQUEST['type']:'';
		$create_time = isset($_REQUEST['create_time'])?$_REQUEST['create_time']:'';
		
		if($create_uid) 
			$sql[] = " create_uid=$create_uid";
		if($title) 
			$sql[] = " title LIKE '%{$title}%' ";
		if($type)
			$sql[] = " type=$type ";
		if($create_time)
			$sql[] = " DATE_FORMAT(create_time, '%Y-%m-%d')='$create_time' ";
		
		return isset($sql)?$sql:'';
	}

	/**
	 * 添加资讯
	 */
	public function add(){
		if(count($_POST) > 0){
			$datas = $this->getFormParams();
			$datas['create_time'] = date('Y-m-d H:i:s');
			$datas['create_uid'] =  $_SESSION['SESSION_USER']['uid'];
			if($this->news_model->add($datas)){
				$this->ajaxReturn(200, '添加成功', '', 'closeCurrent');
			}else{
				$this->ajaxReturn(300, '添加失败');
			}
		}else{
			$this->assign('username', $_SESSION['SESSION_USER']['username']);
			$this->assign('types', $this->types);
			$this->display('Admin/View/News/add.php');
		}
	}
	
	/**
	 * 修改资讯
	 */
	public function update(){
		if(count($_POST) > 0){
			$datas = $this->getFormParams();
			$id = $datas['id'];
			unset($datas['id']);
			$datas['update_time'] = date('Y-m-d H:i:s');
			if($this->news_model->where('id='.$id)->update($datas)){
				$this->ajaxReturn(200, '修改成功', '', 'closeCurrent');
			}else{
				$this->ajaxReturn(300, '修改失败', '');
			}
		}else{
			$news_id = (int)$_REQUEST['id'];
			$news_info = $this->news_model->where("id=$news_id")->find();
			$manager_model = getInstance('Admin\Model\ManagerModel');
			$manager_info = $manager_model->field('username')->where("uid={$news_info['create_uid']}")->find();
			$news_info['create_username'] = $manager_info['username'];
			
			$this->assign('news_info', $news_info);
			$this->assign('types', $this->types);
			$this->display('Admin/View/News/update.php');
		}
	}
	
	/**
	 * 删除资讯
	 */
	public function delete(){
		$ids = $_REQUEST['ids'];
		if(!empty($ids) && $this->news_model->where("id IN($ids)")->delete()){
			$this->ajaxReturn(200, '删除资讯成功');
		}else{
			$this->ajaxReturn(300, '删除资讯失败');
		}
	}
	
	
	/**
	 * 导出至Excel文件
	 */
	public function export(){
		$sql= $this->getWhereSql();
		$data[0] = array('资讯ID', '标题', '内容', '类型', '创建时间', '创建者', '修改时间', '排序值', '点击数');
		$news = $this->news_model->where($sql)->limit(10000)->order('id DESC')->select();
		if(is_array($news)){
			$manager_model = getInstance('Admin\Model\ManagerModel');
			foreach($news as &$n){
				$n['type'] = isset($this->types[$n['type']])?$this->types[$n['type']]:'';
				$manager_info = $manager_model->field('username')->where("uid={$n['create_uid']}")->find();
				$n['create_uid'] = $manager_info['username'];
			}
			$data = array_merge($data, $news);
		}
		import('Util.Excel');
		$xls = new \Excel('UTF-8', false, '资讯列表');
		$xls->addArray($data);
		$xls->generateXML('news_'.date('YmdHis'));
	}
	
    
}
?>